const Mixins = {}
let wraper = require('../component/wraper.vue')
let head = require('../component/head.vue')
let button = require('../component/button.vue')
let page = require('../component/page.vue')
let tabnav = require('../component/tabnav.vue')
let empty = require('../component/empty.vue')
let ImgAuto = require('../component/ImgAuto.vue')
let ImgPreview = require('../component/ImgPreview.vue')





Mixins.install = (Vue, options) => {
	Vue.mixin({
		components: {
			wraper,
			head,
			button,
			ImgPreview,
			page,
			tabnav,
			ImgAuto,
			empty
		},
		data() {
			return {
				modal: {},
				navigator: {},
				isLoading: false,
				isWeb: false,
				isIpx: false,
			}
		},
		filters: {
			formatDateTime(newDatre) {
				if (!newDatre) {
					return ""
				}
				let date = new Date(newDatre.replace(/-/g, "/"));
				// return date;
				var y = date.getFullYear();
				var m = date.getMonth() + 1;
				m = m < 10 ? ('0' + m) : m;
				var d = date.getDate();
				d = d < 10 ? ('0' + d) : d;
				var h = date.getHours();
				h = h < 10 ? ('0' + h) : h;
				var minute = date.getMinutes();
				minute = minute < 10 ? ('0' + minute) : minute;
				var second = date.getSeconds();
				second = second < 10 ? ('0' + second) : second;
				return d + '/' + m + '/' + y + ' ' + h + ':' + minute + ':' + second;
			}
		},
		methods: {

			toast(msg) {
				if (this.modal && this.modal.toast)
					this.modal.toast({
						message: msg
					})
			},
			alert(msg, cb) {
				this.modal.alert({
					message: msg
				}, () => {
					if (cb) {
						cb()
					}
				})
			},
			push(url, param) {
				if (!param)
					this.navigator.push(url)
				else
					this.navigator.pushParam(url, param)
			},
			back(param) {
				if (!param)
					this.navigator.back()
				else
					this.navigator.backFull(param)
			},
			backTo(target) {
				this.navigator.backTo(target)
			},

			getScreenHeight() {
				return 750 / weex.config.env.deviceWidth * weex.config.env.deviceHeight
			},
			/**状态栏高度 */
			getStatusHeight() {
				let top = 0;
				if (!this.isWeb) {
					var page = weex.requireModule("page");
					top = page.statusBarHeight();
				}
				return top
			},
			log(msg, level) {
				if (!level) {
					level = 'info'
				}
				let type = typeof msg
				let log = weex.requireModule('log')
				if (type == 'object') {
					msg = JSON.stringify(msg)
				}
				log.log({
					msg: this.timestamp() + '    ' + msg + '',
					level: level
				})
			},

			timestamp() {
				var date = new Date(); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
				var Y = date.getFullYear() + '-';
				var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
				var D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
				var h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
				var m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
				var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
				return Y + M + D + h + m + s;
			}

		},
		created() {


			const { platform } = weex.config.env;
			this.isWeb = typeof (window) === 'object' && platform.toLowerCase() === 'web';

			if (this.isWeb) {

				this.isIpx = typeof window !== undefined && window.screen && window.screen.width && window.screen.height && (parseInt(
					window.screen.width, 10) === 375) && (parseInt(window.screen.height, 10) === 812);
			} else {

				let deviceHeight = weex.config.env.deviceHeight;
				let deviceWidth = weex.config.env.deviceWidth;

				this.isIpx = weex && ((deviceHeight === 1792 && deviceWidth === 828) || (deviceHeight === 2436 && deviceWidth === 1125) || (deviceHeight === 2688 && deviceWidth === 1242));
			}

			this.navigator = weex.requireModule('navigator')
			this.modal = weex.requireModule('modal')
			var globalEvent = weex.requireModule('globalEvent');
			//全局使用字体图标
			var font = weex.requireModule("font");
			font.addFont('iconfont', 'root:font/font_1280399_1pa6897da0z.ttf')


			globalEvent.addEventListener("onPageInit", (param) => {

				let p = param
				if (param && p.param && p.bubbles)
					p = p.param
				if (this.onLoad != undefined)
					this.onLoad(p)

			});
			globalEvent.addEventListener("viewWillDisappear", (param) => {

				if (this.viewWillDisappear != undefined)
					this.viewWillDisappear()
			});
			globalEvent.addEventListener("viewWillAppear", (param) => {

				if (this.viewWillAppear != undefined)
					this.viewWillAppear()
			});
			globalEvent.addEventListener("onResume", (param) => {

				if (this.onResume != undefined)
					this.onResume()
			});

		}

	})
}
const install = Mixins.install
export {
	install
}
export default Mixins
